package org.freedoit.server.service.auth;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.freedoit.common.util.FreeDoitDataUtils;
import org.freedoit.common.util.SharedConstants;
import org.freedoit.server.dao.DAOFactory;
import org.freedoit.server.om.User;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher dispatcher = request.getRequestDispatcher("/register.jsp");
	    if (dispatcher != null) {
		    dispatcher.forward(request, response);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		System.out.println("username:" + username);
		
		User user = new User();
		user.setUserName(username);
		user.setUserKey(FreeDoitDataUtils.hashString(password));
		user.setStatus(SharedConstants.ACTIVE_INTEGER);
		user.setCreateDate(new Timestamp(System.currentTimeMillis()));
		user.setLatestClientId(new Long(0));
		
		DAOFactory.getUserDAO().persist(user);
	}

}
